package com.hanyao.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hanyao.reggie.bean.User;
import com.hanyao.reggie.common.BaseContext;
import com.hanyao.reggie.common.R;
import com.hanyao.reggie.service.UserService;
import com.hanyao.reggie.utils.SMSUtils;
import com.hanyao.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 寒钥
 * @version 1.0
 */
@RequestMapping("/user")
@RestController
@Slf4j
public class UserController {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private UserService userService;

    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        String phone = user.getPhone();
        log.info("phone={}",phone);
        if(!StringUtils.isEmpty(phone)){
            String s = ValidateCodeUtils.generateValidateCode(4).toString();
            //smsService.sendSms(phone,"瑞吉外卖测试","SMS_474650074","{\"code\":\""+s+"\"}");
            //SMSUtils.sendMessage("瑞吉外卖测试","SMS_474650074",phone,s);
            redisTemplate.opsForValue().set(phone,s,5, TimeUnit.MINUTES);
            //session.setAttribute(phone,s);
            log.info("验证码={}",s);
            return R.success("验证码已发送");
        }
        return R.error("验证码发送失败");
    }
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map ,HttpSession session){
        String phone = (String)map.get("phone");
        log.info("map={}",map);
        String code = map.get("code").toString();
        String s = (String)redisTemplate.opsForValue().get(phone);
        //if(session.getAttribute(phone)!=null&&code.equals(session.getAttribute(phone))){
        log.info("进入了登入验证，phone={},code={}",phone,code);
        if(s!=null&&code.equals(s)){
            LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(User::getPhone,phone);
            User user = userService.getOne(lambdaQueryWrapper);
            if(user==null){
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("user",user.getId());
            redisTemplate.delete(phone);
            log.info("验证成功");
            return R.success(user);
        }
        log.info("验证失败");
        return R.error("登入失败");
    }

}
